﻿using DBMode;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations.Model;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace CMS.ViewModels
{
    public class AddCommodityViewModel : BindableBase, IDialogAware
    {
        private readonly IMethods<Commodity> commodityMethods;
        public AddCommodityViewModel(IMethods<Commodity> CommodityMethods)
        {
            commodityMethods = CommodityMethods;
        }
        public string Title { get; set; }

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
           
        }

        private Commodity commodity=new Commodity();
       

        public Commodity Commodity
        {
            get { return commodity; }
            set { commodity = value; RaisePropertyChanged(); }
        }


        public DelegateCommand ConfirmCommand { get { return new DelegateCommand(Confirm); } }
        public DelegateCommand CancelCommand { get { return new DelegateCommand(Cancel); } }

        private void Cancel()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.No));
        }

        private void Confirm()
        {
            if (Commodity.Name == null || Commodity.Type == null || Commodity.Unit == null || Commodity.Price == null) return;
            int a = commodityMethods.Add(Commodity);
            if (a == 0) MessageBox.Show("添加失败");
            else
            {
                MessageBox.Show("添加成功");
                //IDialogParameters dialogParameters = new DialogParameters();
                //dialogParameters.Add("","")
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            }

        }
    }
}
